const httpsUrl = "https://sanju.xinyunweb.com/apiadmin"
const request = ({url, data, method="POST"})=> {
	data = data || {};
	data.token = wx.getStorageSync('token') || '';
	data.app_id =  10001;
	return new Promise((resolve, reject) => {
		wx.request({
			url: httpsUrl + url,
			data: data,
			dataType: 'json',
			method: method,
			header: {
				// 'content-type': 'application/x-www-form-urlencoded',
				'content-type': 'application/json',
			},
			success: (res) => {
				if (res.statusCode !== 200 || typeof res.data !== 'object') {
				return	reject(res)
				}
				if (res.data.code === -1) {
					// 登录态失效, 重新登录
					reject(res.data)
					return wx.navigateTo({
						url: '/pages/login/login'
					})
				}
				resolve(res.data)
			},
			fail: (res) => {
				reject(res.data)
			},
		})
	})
}
const patchZero = (v) => {
	return ('0' + v).slice(-2)
}

const formatDate = (date) => {
	const [y, m, d] = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
	return `${y}-${patchZero(m)}-${patchZero(d)}`
}
const delay = (time=300) => {
	return new Promise(resolve => {
		setTimeout(()=>resolve(),time)
	})

}

module.exports ={
    request,
	patchZero,
	formatDate,
	delay
}
